package com.wuliu.driver.util;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse;
import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponseBody;
import com.aliyun.oss.OSSClient;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuliu.driver.pojo.DriverRealname;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Map;
import java.util.UUID;

@Component
public class UploadImg {
    @Autowired
    private Oss oss;
    //上传图片到阿里云
    public String upload(MultipartFile file) throws IOException {
        //获取文件名
        String name = file.getOriginalFilename();
        //为了防止客户端传输图片名重复
        String newName = UUID.randomUUID().toString().substring(24)+"---"+name;
        // 创建OSSClient实例，用于与OSS进行交互
        OSSClient xx = new OSSClient(oss.getUrl(), oss.getId(), oss.getSecret());
        // 将文件上传到OSS指定的存储桶中
        xx.putObject(oss.getTong(), newName, file.getInputStream());
        // 构建上传文件的URL
        String url1 = oss.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + oss.getTong() + "." + split[1] + "/" + newName;
        return url;
    }

    //身份证识别
    public DriverRealname idCard(MultipartFile file) throws IOException {
        //获取从前端传输的文件，转换成字节码
        InputStream inputStream = null;
        DriverRealname d = new DriverRealname();
        try {
            inputStream = file.getInputStream();
            //给阿里云准备一个身份验证的请求
            RecognizeIdcardRequest request = new RecognizeIdcardRequest();
            request.setBody(inputStream);
            Config config = new Config().setAccessKeyId(oss.getId()).setAccessKeySecret(oss.getSecret()).setEndpoint("ocr-api.cn-hangzhou.aliyuncs.com");
            //通过client向阿里云发送请求
            Client client = new Client(config);
            //阿里云的响应
            RecognizeIdcardResponse recognize = client.recognizeIdcard(request);
            RecognizeIdcardResponseBody body = recognize.getBody();
            ObjectMapper objectMapper = new ObjectMapper();
            //将阿里云放回的json字符串转换成map对象
            Map m=objectMapper.readValue(body.data,Map.class);
            //解析阿里云返回的数据
            Map data = (Map) m.get("data");
            Map face = (Map) data.get("face");

            if (face!=null) {
                Map o = (Map)face.get("data");
                Object name = o.get("name");
                Object idNum = o.get("idNumber");
                Object address = o.get("address");
                d.setAddress(address.toString());
                d.setName(name.toString());
                d.setIdcard(idNum.toString());
            }else {
                Map map = (Map) m.get("data");
                Map back = (Map) map.get("back");
                Map da = (Map) back.get("data");
                System.out.println(da);
                Object o1 = da.get("validPeriod");
                Object o11 = da.get("issueAuthority");
                d.setExpiration(o1.toString());
                d.setQianf(o11.toString());
            }
            return d;
        } catch (Exception e) {
            e.printStackTrace();
            return d;
        }
    }


}
